const app = getApp()
const util = require('../../utils/util.js')

Page({
    data: {
        statData: {},
        noticeContent: "欢迎使用商家助手，有问题请联系客服解决",
        menuList: [],
        wxAuthUrl: '',
        wxAuthFlag: false
    },
    onLoad: function (options) {
        let webview = options.webview
        //webview为1，表示从webview页面跳转过来的，防止循环跳转
        if (webview != 1) {
            app.formGet('/app/boss/info', null).then(res => {
                if (res.code == 0) {
                    let userInfo = res.data.userInfo
                    let openId = userInfo.openId
                    let mpOpenId = userInfo.mpOpenId
                    if (openId && openId != '') {
                        if (mpOpenId == null || mpOpenId == '') {
                            let redirectUrl = encodeURIComponent('https://www.matforever.cn/es/wx/auth/bind?openId=o7tZU5PJO9XnLxcuC0dK79JBLKdw')
                            let wxAuthUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxffe9b085b70b74f7&redirect_uri=' + redirectUrl +
                                '&response_type=code&scope=snsapi_base&state=#wechat_redirect';
                            this.setData({
                                wxAuthUrl: wxAuthUrl,
                                wxAuthFlag: true
                            })
                        }
                    }
                } else {
                    app.message(res.message, 'error')
                }
            }).catch(e => {
                app.message(e, 'error')
            })
        }
    },
    onShow: function () {
        if (app.globalData.commonMenuList.length == 0) {
            this.getUserCommonMenu()
        } else {
            this.setData({
                menuList: app.globalData.commonMenuList
            })
        }
        this.queryData()
    },
    queryData: function () {
        let _this = this
        app.formGet('/app/index/statistics', this.data.queryParam).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                _this.setData({
                    statData: res.data
                });
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })

        app.formGet('/app/index/notice', null).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                _this.setData({
                    noticeContent: res.data.noticeContent
                });
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    getUserCommonMenu: function () {
        let _this = this
        app.formGet('/app/boss/role/commonMenu', null).then(res => {
            if (res.code == 0) {
                app.globalData.commonMenuList = res.data.list
                this.setData({
                    menuList: res.data.list
                })
            } else {
                console.log("获取用户菜单失败，%o" + res)
            }
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        console.log("aa")
    }
})
